// Fluxions 3D Engine
// vector3.hpp
// Copyright (C) 2000-2011 Jonathan Metzgar
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifndef FLUXIONS_IMAGE_HPP
#define FLUXIONS_IMAGE_HPP

#include <string>
#include <fstream>
#include "opengl.hpp"

namespace Fluxions
{
	class Image
	{
	public:
		Image();
		~Image();

		bool LoadTEX(const std::string &filename);
		void SaveTEX(const std::string &filename);
		void Upload();

		int width;
		int height;
		int bpp;
	private:
		int components;
		GLenum format;
		GLenum type;
		void *data;
	};

	Image::Image()
		: data(0)
	{
	}

	Image::~Image()
	{
		if (data)
			free(data);
	}

	bool Image::LoadTEX(const std::string &filename)
	{
		FILE *fin = fopen(filename.c_str(), "rb");
		if (!fin)
			return false;
		fread(&width, sizeof(int), 1, fin);
		fread(&height, sizeof(int), 1, fin);
		fread(&bpp, sizeof(int), 1, fin);
		data = malloc(bpp/8*width*height);
		fread(data, 1, bpp/8*width*height);
		fin.close();
		if (bpp == 24)
		{
			components = 3;
			format = GL_RGB;
			type = GL_UNSIGNED_BYTE;
		}
		else if (bpp == 32)
		{
			components = 4;
			format = GL_RGBA;
			type = GL_UNSIGNED_BYTE;
		}
		return true;
	}

	bool Image::SaveTEX(const std::string &filename)
	{

		return true;
	}

	void Image::Upload()
	{
		gluBuild2DMipmaps(GL_TEXTURE_2D, components, width, height, format, type, data);
	}
}

#endif
